﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AcctrueWMS.Foundation.Util
{
    public static class DataHelper
    {
        public static string CheckForNull(string val)
        {
            return CheckForNull(val, string.Empty);
        }
        public static object CheckForNull(object obj)
        {
            return obj==null?string.Empty:obj;
        }
        public static string CheckForNull(string val, string rpl)
        {
            return val ?? rpl;
        }

        private static char[] illegalChars = new char[] {'\n','\t','\r','~','(',')','#','\\','/','=','>',
            '<','+','-','*','%','&','|','^',',','"','[',']'};
        /// <summary>
        /// 过滤DataTable.Select方法FilterExpression中的非法字符
        /// </summary>
        /// <param name="expr"></param>
        /// <returns></returns>
        public static string FilterDbExpression(string expr)
        {
            char[] strChars = expr.ToCharArray();

            for (int i = 0; i < strChars.Length;i++ )
            {
                foreach (char ic in illegalChars)
                {
                    if (strChars[i] == ic)
                    {
                        strChars[i] = char.MinValue;
                        break;
                    }
                }
            }
            int cnt = strChars.Length;
            for (int i = 0; i < strChars.Length; i++)
            {
                if (strChars[i] == char.MinValue)
                {
                    cnt--;
                    for (int j =i+1; j <strChars.Length; j++)
                    {
                        if (strChars[j] != char.MinValue)
                        {
                            strChars[i] = strChars[j];
                            strChars[j] = char.MinValue;
                            break;
                        }
                    }
                }
            }
            return new string(strChars,0,cnt);
        }
    }
}
